$(function(){
	
	var dropbox = $('.drop-box');
	
	dropbox.filedrop({
        paramname:'pic',        // The name of the $_FILES entry:
        withCredentials: true,	// make a cross-origin request with cookies
        data: {
			paramkey: 'pic', 			// send POST variables
		},
        //allowedfiletypes: ['image/jpeg','image/png','image/gif'],	// filetypes allowed by Content-Type.  Empty array means no restrictions
		maxfiles: 1,
    	maxfilesize: 16,         // max file size in MBs
		url: '?ct=content&ac=upload',
		
        uploadFinished: function(dom, i, file, response, time) {
            //console.log($(obj).attr('rel'));
            $('.preview', dom).addClass('done');
			//$.data(file).addClass('done');
			// response is the JSON object that post_file.php returns
            $(".drop-file", dom).val(response.filename);
            //console.log(i + " === " + JSON.stringify(file) + " === " + JSON.stringify(response) + ' === ' + time);
		},
    	error: function(err, file) {
			switch(err) {
				case 'BrowserNotSupported':
					showMessage('您的浏览器不支持HTML5拖动上传!');
					break;
				case 'TooManyFiles':
					showMessage('只能同时上传5个文件');
					break;
				case 'FileTooLarge':
					showMessage('文件太大，只允许上传16M的文件.');
					break;
                case 'FileTypeNotAllowed':
					showMessage('文件格式不支持.');
                    break;
				default:
					break;
			}
		},
		
		// Called before each upload is started
		beforeEach: function(file){
			if(!file.type.match(/^image\//)){
				//alert('Only images are allowed!');
				
				// Returning false will cause the
				// file to be rejected
				//return false;
			}
		},
		
		uploadStarted:function(dom, i, file, len){
			createPreview(file, dom);
		},
		
		progressUpdated: function(dom, i, file, progress) {
            $(".progress", dom).width(progress);
			//$.data(file).find('.progress').width(progress);
		},

        speedUpdated: function(i, file, speed) {
			// speed in kb/s
            //console.log(speed+" kb/s");
            //$(".progress").html(parseInt(speed)+" kb/s");
		},

    	 
	});
	
	var template = '<div class="preview">'+
						'<span class="imageHolder">'+
							'<img />'+
							'<span class="uploaded"></span>'+
						'</span>'+
						'<div class="progressHolder">'+
							'<div class="progress"></div>'+
						'</div>'+
					'</div>'; 
	
	
	function createPreview(file, dom){

        $(".preview", dom).remove();
		var preview = $(template); 
        if ($(dom).attr('rel') == 'image'){

            var image = $('img', preview);

            var reader = new FileReader();

            image.width = 100;
            image.height = 100;

            reader.onload = function(e){

                // e.target.result holds the DataURL which
                // can be used as a source of the image:

                image.attr('src', e.target.result);
            };

            // Reading the file as a DataURL. When finished,
            // this will trigger the onload function above:
            reader.readAsDataURL(file);
        }
		
		$('.drop-text', dom).hide();
		preview.appendTo($(dom));
		
		// Associating a preview container
		// with the file, using jQuery's $.data():
		
		$.data(file,preview);
	}

	function showMessage(msg){
		message.html(msg);
	}

});

function check_form() {
    if ($(".drop-image").val()=='') {
        alert("图片还在上传");
        return false;
    };
    return true;
} 
